package com.rdzn.mall.center.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.rdzn.mall.base.util.DateTimeUtils;
import com.rdzn.mall.center.entity.OmsOrder;
import com.rdzn.mall.center.entity.OmsOrderItem;
import com.rdzn.mall.center.entity.PmsSkuStock;
import com.rdzn.mall.center.enums.OrderStatus;
import com.rdzn.mall.center.mapper.OmsOrderMapper;
import com.rdzn.mall.center.service.IOmsOrderItemService;
import com.rdzn.mall.center.service.IOmsOrderService;
import com.rdzn.mall.center.service.IPmsSkuStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: oms_order
 * @Author: jeecg-boot
 * @Date:   2020-08-27
 * @Version: V1.0
 */

@Service
public class OmsOrderServiceImpl extends ServiceImpl<OmsOrderMapper, OmsOrder> implements IOmsOrderService {

    @Autowired
    private IOmsOrderItemService orderItemService;
    @Autowired
    private IPmsSkuStockService skuStockService;

    @Override
    public List<OmsOrder> list(Integer memberId, Integer status) {
        if (status == null){
            return list(new LambdaQueryWrapper<OmsOrder>()
                    .eq(OmsOrder::getMemberId, memberId)
                    .eq(OmsOrder::getDeleteStatus, 0)
                    .lt(OmsOrder::getStatus, 4)
                    .orderByDesc(OmsOrder::getCreateTime)
            );
        }else {
            return list(new LambdaQueryWrapper<OmsOrder>()
                    .eq(OmsOrder::getMemberId, memberId)
                    .eq(OmsOrder::getStatus, status)
                    .eq(OmsOrder::getDeleteStatus, 0)
                    .orderByDesc(OmsOrder::getCreateTime)
            );
        }
    }

    @Override
    public List<OmsOrder> listByIdAndStatus(Integer id, Integer status) {
        return list(new LambdaQueryWrapper<OmsOrder>()
                .eq(OmsOrder::getId, id)
                .eq(OmsOrder::getStatus, status)
                .eq(OmsOrder::getDeleteStatus, 0)
        );
    }

    @Override
    public List<OmsOrder> getTimeOutOrders(Integer minute) {
        return list(new LambdaQueryWrapper<OmsOrder>().eq(OmsOrder::getStatus, 0)
                .lt(OmsOrder::getCreateTime, DateTimeUtils.minusMinutes(minute, null))
        );
    }

    @Override
    public OmsOrder onPaySuccess(OmsOrder order) {
        //1、更新订单状态
        order.setStatus(OrderStatus.WAIT_SEND.CODE);
        this.updateById(order);
        //2、修改库存
        List<OmsOrderItem> orderItemList = orderItemService.listByOrderId(order.getId());
        if (CollectionUtils.isNotEmpty(orderItemList)){
            orderItemList.forEach(omsOrderItem -> {
                PmsSkuStock skuStock = skuStockService.getById(omsOrderItem.getProductSkuId());
                if (skuStock != null){
                    skuStock.setStock(skuStock.getStock() - omsOrderItem.getProductQuantity());
                    skuStock.setUpdateTime(new Date());
                    skuStockService.updateById(skuStock);
                }
            });
        }
        return order;
    }

    @Override
    public OmsOrder getByOrderSn(String orderSn) {
        return getOne(new LambdaQueryWrapper<OmsOrder>().eq(OmsOrder::getOrderSn, orderSn));
    }
}
